package com.www.applyword.ui.home;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.www.applyword.Constants.Constants;
import com.www.applyword.R;
import com.www.applyword.adapter.SearchHotAdapter;
import com.www.applyword.base.BaseActivity;
import com.www.applyword.bean.BannerBean;
import com.www.applyword.bean.OrderBean;
import com.www.applyword.http.CallBack;
import com.www.applyword.http.HttpClient;
import com.www.applyword.utils.ListDataSave;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * author : huang
 * date   : 2019/11/20 0020 9:43
 * version: 1.0
 */
public class SearchActivity extends BaseActivity {
    @BindView(R.id.message_search)
    EditText query;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;
    @BindView(R.id.tv_clear)
    TextView tv_clear;
    @BindView(R.id.rl_job_search)
    RecyclerView rlJobSearch;
    @BindView(R.id.rl_job_search2)
    RecyclerView rlJobSearch2;
    private BannerBean.HotSearchBean hotSearch;
    private String itemValue;
    private String[] split;
    private SearchHotAdapter searchHotAdapter2;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.search_activity);
        setHead_title(View.GONE);
        hotSearch = (BannerBean.HotSearchBean) getIntent().getSerializableExtra("hotSearch");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rlJobSearch.setLayoutManager(layoutManager);
        SearchHotAdapter searchHotAdapter = new SearchHotAdapter(this);
        rlJobSearch.setAdapter(searchHotAdapter);

        LinearLayoutManager layoutManager2 = new LinearLayoutManager(this);
        layoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
        rlJobSearch2.setLayoutManager(layoutManager2);
        searchHotAdapter2 = new SearchHotAdapter(this);
        rlJobSearch2.setAdapter(searchHotAdapter2);
        searchHotAdapter2.addAll(ListDataSave.getInstence(SearchActivity.this).getDataList());

        itemValue = hotSearch.getItemValue();
        split = itemValue.split(",");
        searchHotAdapter.addAll(Arrays.asList(split));
        searchHotAdapter.setOnItemClickListener((viewHolder, view, i) -> {
            startActivityForResult(new Intent(SearchActivity.this, HotJobActivity.class).putExtra("Keyword", split[i]),100);
            ListDataSave.getInstence(SearchActivity.this).addDataList(split[i]);
        //    SPUtil.put(Constants.SEARCH, ListDataSave.getInstence(SearchActivity.this).setData(split[i])));
           // query.setText(split[i]);
         // query.setSelection(split[i].length());
        });
    }

    @Override
    protected void loadData() {
        query.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //点击搜索的时候隐藏软键盘
                 // hideKeyboard(EditText);
                // 在这里写搜索的操作,一般都是网络请求数据
                if (!TextUtils.isEmpty(v.getText().toString().trim()))
                    startActivityForResult(new Intent(SearchActivity.this, HotJobActivity.class).putExtra("Keyword", v.getText().toString().trim()),100);
                ListDataSave.getInstence(SearchActivity.this).addDataList(v.getText().toString().trim());
             //   getFollowList(v.getText().toString().trim());
                return true;
            }
            return false;
        });
    }

    private void getFollowList(String trim) {
        Map<String, Object> params = new HashMap<>();
        params.put("Page","1");
        params.put("Rows","20");
        params.put("Keyword",trim);
        HttpClient.get(SearchActivity.this, Constants.INDEXHOTSEARCHORDERLIST, params,new CallBack<List<OrderBean>>() {
            @Override
            public void onSuccess(List<OrderBean> orderBeans) {

            }
        });
    }


    @OnClick({R.id.message_search, R.id.tv_cancel,R.id.tv_clear})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.message_search:

                break;
            case R.id.tv_cancel:
                finish();
                break;
            case R.id.tv_clear:
                ListDataSave.getInstence(SearchActivity.this).removeDificult();
                searchHotAdapter2.replaceAll(new ArrayList<>(),true);
                break;
            default:
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 100: //返回的结果是来自于Activity B
                searchHotAdapter2.replaceAll(ListDataSave.getInstence(SearchActivity.this).getDataList(),true);
                break;
            default:
                break;
        }
    }
}
